﻿/**
* 移动版Datepicker汉化版 plugin
* 主要实现日期选择功能
* @name Datepicker
* @author DingYafei - http://www.5niu.org（dgunzi http://www.5niu.org）
* @version 0.1
* @copyright (c) 2011 DingYafei (5niu.org)
*/
datepicker = {
    cd: new Date(),
    current: null,
    windialog: null,
    /*Defaults*/
    initTime: function() {
        datepicker.cd = new Date();
        $('#dStr').html(dateFormat(datepicker.cd, "yyyy年mm月dd日 dddd"));
        $('#mon').val(dateFormat(datepicker.cd, "mmm"));
        $('#day').val(dateFormat(datepicker.cd, "dd"));
        $('#year').val(dateFormat(datepicker.cd, "yyyy"));
    },
    initDialog: function() {
        var top = ($(window).height() - $('#dialog').height()) / 2;
        $(window).resize(function(t) {
            if (top < 0) { top = 0; }
            $('#dialog').css('left', ($(window).width() - $('#dialog').width()) / 2 + 'px').css('top', top + 'px');
        });
        $('#dialog').css('left', ($(window).width() - $('#dialog').width()) / 2 + 'px').css('top', top + 'px');
        $('#dialog').jqm({ modal: true });
    },
    /*Functions*/
    updateF: function() {
        $('#year').val(dateFormat(datepicker.cd, "yyyy"));
        $('#mon').val(dateFormat(datepicker.cd, "mmm"));
        $('#day').val(dateFormat(datepicker.cd, "dd"));
        $('#dStr').html(dateFormat(datepicker.cd, "yyyy年mm月dd日 dddd"));
        if (navigator.userAgent.indexOf("Firefox") == -1) {
            $('#setfoc').focus();
        }
        return false;
    },
    init: function() {
        /*Mods*/
        $('#pyear').tap(function() {
            datepicker.cd.setYear(datepicker.cd.getFullYear() + 1);
            datepicker.updateF();
            return false;
        });
        $('#pmon').tap(function() {
            datepicker.cd.setMonth(datepicker.cd.getMonth() + 1);
            datepicker.updateF();
            return false;
        });
        $('#pday').tap(function() {
            datepicker.cd.setDate(datepicker.cd.getDate() + 1);
            datepicker.updateF();
            return false;
        });
        $('#myear').tap(function() {
            datepicker.cd.setYear(datepicker.cd.getFullYear() - 1);
            datepicker.updateF();
            // $('#year').focus();
            return false;
        });
        $('#mmon').tap(function() {
            datepicker.cd.setMonth(datepicker.cd.getMonth() - 1);
            datepicker.updateF();
            return false;
        });
        $('#mday').tap(function() {
            datepicker.cd.setDate(datepicker.cd.getDate() - 1);
            datepicker.updateF();
            return false;
        });
        $('#close').tap(function() {
            datepicker.windialog.close();
            return false;
        });
        $('#set').tap(function(event) {
            $('#' + datepicker.current).val(dateFormat(datepicker.cd, "yyyy-mm-dd"));
            datepicker.windialog.close();
            return false;
        });
    },
    remove: function() {
        $('#pyear').unbind();
        $('#pmon').unbind();
        $('#pday').unbind();
        $('#myear').unbind();
        $('#mmon').unbind();
        $('#mday').unbind();
        $('#close').unbind();
        $('#set').unbind();
    },
    handler: function(event) {
        datepicker.current = event.data.curdatetext;
        $('#setfoc').simpledialog({ 'content': '<div class="jqmWindow" id="dialog"><table class="datepickertable" cellpadding="0" cellspacing="0" border="0"><tr><td colspan="3"><div style="border-bottom:thin solid #fff;font-size:18px;margin-bottom:5px;padding:5px;height:50px;"><div style="float:left;margin-right:5px;"><img src="11-clock.png"/></div><div id="dStr" style="width:200px;float:left;font-size:16px;margin-top:3px;letter-spacing:1px;font-weight:lighter; font-family: Helvetica, Arial, sans-serif;"></div></div></td></tr><tr><td><table align="center" style="margin-top:5px;margin-bottom:5px;" cellpadding="0"  cellspacing="5" border="0"><tr><td><div id="pyear" role="main" class="ui-content ui-body-b ui-corner-top-2" data-role="content" style="text-align: center; width: 79px; padding: 5px 0px;margin:0px 2px;cursor:pointer;">+</div><input type="text" class="ui-shadow-inset ui-body-c" id="year" style="width:76px;padding:0px;height:38px;margin:0px 2px;" /><div id="myear" role="main" class="ui-content ui-body-b ui-corner-bottom-2" data-role="content" data-theme="a" style="text-align: center; width: 79px; padding: 5px 0px;margin:0px 2px;cursor:pointer;">-</div></td><td><div id="pmon"  role="main" class="ui-content ui-body-b ui-corner-top-2" data-role="content" style="text-align: center; width: 63px; padding: 5px 0px;margin:0px 2px;cursor:pointer;">+</div><input type="text" class="ui-shadow-inset ui-body-c" id="mon" style="width:60px;padding:0px;height:38px;margin:0px 2px;" /><div id="mmon" role="main" class="ui-content ui-body-b ui-corner-bottom-2" data-role="content" data-theme="a" style="text-align: center; width: 63px; padding: 5px 0px;margin:0px 2px 2px 2px;cursor:pointer;">-</div></td><td><div id="pday" role="main" class="ui-content ui-body-b ui-corner-top-2" data-role="content"  style="text-align: center; width: 63px; padding: 5px 0px;margin:0px 2px;cursor:pointer;">+</div><input type="text" class="ui-shadow-inset ui-body-c" id="day" style="width:60px;padding:0px;height:38px;margin:0px 2px;" /><div id="mday" role="main" class="ui-content ui-body-b ui-corner-bottom-2" data-role="content" data-theme="a" style="text-align: center; width: 63px; padding: 5px 0px;margin:0px 2px; cursor:pointer;">-</div></td></tr></table></td></tr><tr><td colspan="3" style="background-color:#a7a7a7;"><table width="100%"><tr><td width="50%"><div role="main" id="set" class="ui-content ui-body-b ui-corner-all-1" data-role="content" data-theme="a" style="font-size:20px;text-align: center; padding: 10px 0px;margin:2px;cursor:pointer;">确定</div></td><td><div role="main" id="close" class="ui-content ui-body-b ui-corner-all-1" data-role="content" data-theme="a" style="font-size:20px;text-align: center; padding: 10px 0px;margin:2px;cursor:pointer;">取消</div></td></tr></table></td></tr></table></div> ' });
        datepicker.initTime();
        if (navigator.userAgent.indexOf("Firefox") == -1) {
            $('#setfoc').focus();
        }
        return false;
    }
};